pid_get_parent: fix potential leak of kp
authorAntoine Jacoutot <ajacoutot@openbsd.org>
Fri, 17 May 2019 11:55:16 +0000 (11:55 +0000)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 17 May 2019 11:55:16 +0000 (11:55 +0000)
gtk/gtkmountoperation-x11.c

index 1b7bff6aa1c8cc7202edbfc57b91bb874a4a1498..268354f31bc746416a2712ea170e16d8a73b5066 100644 (file)
@@ -736,25 +736,32 @@ pid_get_command_line (GPid pid)
 static GPid
 pid_get_parent (GPid pid)
 {
-  struct kinfo_proc *kp;
+  struct kinfo_proc *kp = NULL;
   size_t len;
-  GPid ppid;
+  GPid ppid = 0;
+
+  /* fail if trying to get the parent of the init process (no such thing) */
+  if (pid == 1)
+      goto out;
 
   int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid,
                 sizeof(struct kinfo_proc), 0 };
 
   if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1)
-      return (-1);
+      goto out;
+
   mib[5] = (len / sizeof(struct kinfo_proc));
 
   kp = g_malloc0 (len);
 
   if (sysctl (mib, G_N_ELEMENTS (mib), kp, &len, NULL, 0) < 0)
-      return -1;
+      goto out;
 
   ppid = kp->p_ppid;
 
-  g_free (kp);
+out:
+  if (kp)
+      g_free (kp);
   return ppid;
 }